(function(){
    // var origin = "http://www.gojiw.com/gojiw/index/index_recycle/index.php../../index_recycle/index.php/Admin/Ajax/get_brand_version"
    var Accordion = function(el, multiple) {
		this.el = el || {};
		this.multiple = multiple || false;

		// Variables privadas
		var links = this.el.find('.link');
		// Evento
		links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown)
	}

	Accordion.prototype.dropdown = function(e) {
		var $el = e.data.el;
			$this = $(this),
			$next = $this.next();

		$next.slideToggle();
		$this.parent().toggleClass('open');

		if (!e.data.multiple) {
			$el.find('.submenu').not($next).slideUp().parent().removeClass('open');
		};
	}

    var app = new Vue({
        el: "#app-root",
        data: {
            brands: [],
            categorys: [],

            b_id: '',
            v_id: '',

            c_title: '',
            o_title: '',

            v_max_price: '',
            v_min_price: '',

            c_name: '',
            o_name: '',

            o_require: '0', 
            o_radio: '0',
            o_price: '0',

            operation: [],
            
            error: 0,
            error_message: ''
        },
        created: function(){
            var _this = this;
            $.ajax({
                url: '../../index_recycle/index.php/Admin/Ajax/get_brand_version',
                type: 'GET',
                dataType: 'json',
                success: function(redata){
                    if (redata.code == 200) {
                        _this.brands = redata.data;
                        setTimeout(function(){
                            new Accordion($('#accordion'), false);
                        },0);
                    } else {
                        console.log(redata.message);
                    }
                },
                error: function(e){
                    console.log(e);
                }
            });
        },
        methods: {
            brand_selected: function(id){
                if (id !== undefined) this.b_id = id;
            },
            version_selected: function(id, e){
                if (e) $('#accordion').removeClass('now');
                if (e) $(e.target).addClass('now');
                var _this = this;
                if (id !== undefined) this.v_id = id;
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/get_option_category',
                    type: 'POST',
                    dataType: 'json',
                    data: { vid: this.v_id },
                    success: function(redata){
                        if (redata.code == 200) {
                            var categorys = redata.data;
                            _this.categorys = categorys.option_category;
                            _this.v_max_price = categorys.recycle_cost.v_max_price;
                            _this.v_min_price = categorys.recycle_cost.v_min_price;
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
                
            },
            set_max_price: function(){
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/set_max_price',
                    type: 'POST',
                    dataType: 'json',
                    data: { 
                        vid: this.v_id,
                        max_price: this.v_max_price
                    },
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('设置成功');
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            set_min_price: function(){
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/set_min_price',
                    type: 'POST',
                    dataType: 'json',
                    data: { 
                        vid: this.v_id,
                        min_price: this.v_min_price
                    },
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('设置成功');
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            edit_option: function(operation){
                this.c_title = '编辑选项';
                this.error = 0;
                this.error_message = '';
                $('#optionModal').modal('show');
                this.operation = operation;
            },
            add_category: function(){
                this.c_title = '编辑选项';
                this.error = 0;
                this.error_message = '';
                $('#categoryModal').modal('show');
            },
            update_category: function(id){
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/edit_option_category',
                    type: 'POST',
                    dataType: 'json',
                    data: { 
                        cid: id,
                        require: ($("#category-require-" + id).is(':checked') ? 1 : 0),
                        radio: ($("#category-radio-" + id).is(':checked') ? 1 : 0)
                    },
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('更新成功');
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            delete_option: function(id){
                var _this = this;
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/del_option',
                    type: 'POST',
                    dataType: 'json',
                    data: { oid: id },
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('删除成功');
                            _this.version_selected();
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            delete_category: function(id){
                var _this = this;
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/del_option_category',
                    type: 'POST',
                    dataType: 'json',
                    data: { cid: id },
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('删除成功');
                            _this.version_selected();
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            submitOption: function(){
                var _this = this;
                if (!/^[\d]+$/g.test(this.o_price) || !this.o_name){
                    this.error = 1;
                    this.error_message = '请输入正确参数。';
                    return false;
                }
                var url = '../../index_recycle/index.php/Admin/Ajax/add_option';
                var data = {
                    label: this.o_name,
                    price: this.o_price
                }
                if (this.operation[0] === 'update') {
                    data.oid = this.operation[1];
                    url = '../../index_recycle/index.php/Admin/Ajax/edit_option';
                }else{
                    data.cid = this.operation[1];
                }
                $.ajax({
                    url: url,
                    type: 'POST',
                    dataType: 'json',
                    data: data,
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('成功');
                            _this.version_selected();
                            $('#optionModal').modal('hide');
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            },
            submitCategory: function(){
                var _this = this;
                if (!this.c_name) {
                    this.error = 1;
                    this.error_message = '请输入选项名称。';
                    return false;
                }
                var data = {
                    vid: this.v_id,
                    name: this.c_name,
                    require: this.o_require,
                    radio: this.o_radio
                }
                $.ajax({
                    url: '../../index_recycle/index.php/Admin/Ajax/add_option_category',
                    type: 'POST',
                    dataType: 'json',
                    data: data,
                    success: function(redata){
                        if (redata.code == 200) {
                            console.log('成功');
                            $('#categoryModal').modal('hide');
                            _this.version_selected();
                        } else {
                            console.log(redata.message);
                        }
                    },
                    error: function(e){
                        console.log(e);
                    }
                });
            }
        }
    });
})();